package util

import (
	"github.com/go-ping/ping"
	"github.com/google/uuid"
	"log"
	"time"
)

func IsPing(target string) bool {
	pinger, err := ping.NewPinger(target)
	if err != nil {
		panic(err)
	}

	pinger.Count = 1
	pinger.Timeout = 1 * time.Second
	pinger.Run() // blocks until finished

	stats := pinger.Statistics()

	// 有回包，就是说明IP是可用的
	if stats.PacketsRecv >= 1 {
		return true
	}

	return false
}

// get a random UUID.
func GetUUID() string {
	u, err := uuid.NewRandom()
	if err != nil {
		log.Println(err)
	}
	return u.String()
}

